package com.bw.zhuangao5___day002.UI.Reister;


import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.mvp.V.BannerFragment;
import com.bw.zhuangao5___day002.Activity.JanewyerActivity;
import com.bw.zhuangao5___day002.Activity.MainActivity;
import com.bw.zhuangao5___day002.Adpter.BaseAdpter.BooflassAdpter;
import com.bw.zhuangao5___day002.Apk.App;
import com.bw.zhuangao5___day002.Glide.Glides;
import com.bw.zhuangao5___day002.JavaClass.ZhangAndReister2;
import com.bw.zhuangao5___day002.JavaClass.ZhangAndReister3;
import com.bw.zhuangao5___day002.R;
import com.bw.zhuangao5___day002.SQLline.SQLcontion;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.content.Context.NOTIFICATION_SERVICE;

public class BlankFragmentReister extends BannerFragment {

    Timer timer;
    int shijian = 10;
    int suiji;
    BooflassAdpter adpter;
    int qiehuan = 0;
    ZhangAndReister2 xiaomaii;
    private ImageView BlankFragmentReisterImageView;
    private EditText BlankFragmentReisterEditText1;
    private EditText BlankFragmentReisterEditText2;
    private CheckBox BlankFragmentReisterCheckBox1;
    private EditText BlankFragmentReisterEditText3;
    private CheckBox BlankFragmentReisterCheckBox2;
    private Button BlankFragmentReisterReister;
    private Button BlankFragmentReisterLogin;
    private TextView BlankFragmentReisterTextView;
    private RecyclerView BlankFragmentReisterRecyclerView;
    private EditText BlankFragmentReisterEditText4;
    private Button BlankFragmentReisterButton;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100) {
                shijian--;
                BlankFragmentReisterButton.setText("倒计时：" + shijian);
                BlankFragmentReisterButton.setEnabled(false);
                if (shijian == 5) {

                    suiji = (int) ((Math.random() * 9 + 1) * 100000);
                    Notification.Builder builder = new Notification.Builder(getActivity());
                    builder.setSmallIcon(R.mipmap.ic_launcher);
                    builder.setContentTitle("短信验证");
                    builder.setContentText("" + suiji);
                    NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
                    notificationManager.notify(1, builder.build());
                }
                if (shijian == 0) {
                    timer.cancel();
                    shijian = 10;
                    handler.sendEmptyMessage(200);
                }

            }
            if (msg.what == 200) {
                BlankFragmentReisterButton.setEnabled(true);
                BlankFragmentReisterButton.setText("重新获取");
                BlankFragmentReisterEditText4.setText("" + suiji);
            }
        }
    };

    @Override
    public int bandLayoud() {
        return R.layout.fragment_blank_fragment_reister;
    }

    @Override
    public void initView() {
        BlankFragmentReisterImageView = (ImageView) findViewById(R.id.BlankFragmentReister_ImageView);
        BlankFragmentReisterEditText1 = (EditText) findViewById(R.id.BlankFragmentReister_EditText1);
        BlankFragmentReisterEditText2 = (EditText) findViewById(R.id.BlankFragmentReister_EditText2);
        BlankFragmentReisterCheckBox1 = (CheckBox) findViewById(R.id.BlankFragmentReister_CheckBox1);
        BlankFragmentReisterEditText3 = (EditText) findViewById(R.id.BlankFragmentReister_EditText3);
        BlankFragmentReisterCheckBox2 = (CheckBox) findViewById(R.id.BlankFragmentReister_CheckBox2);
        BlankFragmentReisterReister = (Button) findViewById(R.id.BlankFragmentReister_Reister);
        BlankFragmentReisterLogin = (Button) findViewById(R.id.BlankFragmentReister_Login);
        BlankFragmentReisterTextView = (TextView) findViewById(R.id.BlankFragmentReister_TextView);
        BlankFragmentReisterRecyclerView = (RecyclerView) findViewById(R.id.BlankFragmentReister_RecyclerView);
        BlankFragmentReisterRecyclerView.setLayoutManager(new LinearLayoutManager(App.context));

        BlankFragmentReisterEditText4 = (EditText) findViewById(R.id.BlankFragmentReister_EditText4);//短信输入框
        BlankFragmentReisterButton = (Button) findViewById(R.id.BlankFragmentReister_Button);//获取短信按钮
    }

    @Override
    public void initDate() {


        BlankFragmentReisterImageView.setVisibility(View.GONE);


        BlankFragmentReisterTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (qiehuan % 2 != 0) {
                    BlankFragmentReisterRecyclerView.setVisibility(View.GONE);
                } else {
                    BlankFragmentReisterRecyclerView.setVisibility(View.VISIBLE);
                }
                ++qiehuan;
                Log.i("aaaaaaa", "" + qiehuan);
            }
        });


        BlankFragmentReisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(100);
                    }
                }, 0, 1000);
            }
        });
        BlankFragmentReisterReister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(BlankFragmentReisterEditText1.getText().toString()) && !TextUtils.isEmpty(BlankFragmentReisterEditText2.getText().toString()) && !TextUtils.isEmpty(BlankFragmentReisterEditText3.getText().toString())) {
                    if (BlankFragmentReisterEditText2.getText().toString().equals("" + BlankFragmentReisterEditText3.getText().toString())) {
                        Alers();
                    } else {
                        Toast.makeText(getActivity(), "车牌号必须一致", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getActivity(), "请老司机完善购买信息", Toast.LENGTH_SHORT).show();
                }

            }
        });

        Glides.Yuanjiao(getActivity(), R.drawable.dong3, BlankFragmentReisterImageView, 50);

        Domiss();

        BlankFragmentReisterLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JanewyerActivity.day2();
            }
        });

        BlankFragmentReisterCheckBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    BlankFragmentReisterEditText2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    BlankFragmentReisterEditText2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                BlankFragmentReisterEditText2.setSelection(BlankFragmentReisterEditText2.length());
            }
        });

        BlankFragmentReisterCheckBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    BlankFragmentReisterEditText3.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    BlankFragmentReisterEditText3.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                BlankFragmentReisterEditText3.setSelection(BlankFragmentReisterEditText3.length());
            }
        });


    }

    public void Domiss() {
        SQLcontion sqLcontimiss = new SQLcontion(getActivity(), "ShuJu.db", null, 1);
        SQLiteDatabase writmiss = sqLcontimiss.getWritableDatabase();
        Cursor sqsq = writmiss.query("ShuJu", null, null, null, null, null, null);
        List<ZhangAndReister3> kaixi3 = new ArrayList<>();
        while (sqsq.moveToNext()) {
            String name = sqsq.getString(sqsq.getColumnIndex("zhanghao"));
            String pasw = sqsq.getString(sqsq.getColumnIndex("mima"));
            ZhangAndReister3 kaixinba3 = new ZhangAndReister3(name, pasw);
            kaixi3.add(kaixinba3);
            adpter = new BooflassAdpter(kaixi3);
            BlankFragmentReisterRecyclerView.setAdapter(adpter);
        }
    }

    public void Alers() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("老司机购车！");
        builder.setIcon(R.mipmap.anli);
        builder.setMessage("车型：" + BlankFragmentReisterEditText1.getText().toString() + "    车牌号：" + BlankFragmentReisterEditText2.getText().toString());
        builder.setPositiveButton("确定购买?", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                BlankFragmentReisterImageView.setVisibility(View.VISIBLE);


                String username2 = BlankFragmentReisterEditText1.getText().toString().trim();
                String password2 = BlankFragmentReisterEditText2.getText().toString().trim();
                SQLcontion sqLcontionViss = new SQLcontion(getActivity(), "ShuJu.db", null, 1);
                SQLiteDatabase writViss = sqLcontionViss.getWritableDatabase();
                Cursor sqsq = writViss.query("ShuJu", null, null, null, null, null, null);
                List<ZhangAndReister2> kaixinbas2 = new ArrayList<>();
                while (sqsq.moveToNext()) {
                    String name = sqsq.getString(sqsq.getColumnIndex("zhanghao"));
                    String pasw = sqsq.getString(sqsq.getColumnIndex("mima"));
                    ZhangAndReister2 kaixinba2 = new ZhangAndReister2(name, pasw);
                    kaixinbas2.add(kaixinba2);
                }
                boolean iskao = false;
                for (int i = 0; i < kaixinbas2.size(); i++) {
                    xiaomaii = kaixinbas2.get(i);
                    if (username2.equals(xiaomaii.getZhanghao2())) {
                        iskao = true;
                    } else {
                        iskao = false;
                    }
                }

                //-----------------------------开始------------------------------------------------------------------
                if (iskao) {
                    Log.i("数据", "" + kaixinbas2.size());
                    Toast.makeText(getActivity(), "相同车型不能再购买", Toast.LENGTH_SHORT).show();
                } else {
                    Log.i("数据", "" + kaixinbas2.size());
                    //--------------------------
                    SQLcontion sqLcontion = new SQLcontion(getActivity(), "ShuJu.db", null, 1);
                    SQLiteDatabase writableDatabase = sqLcontion.getWritableDatabase();
                    writableDatabase.execSQL("insert into ShuJu values(?,?)", new Object[]{
                            BlankFragmentReisterEditText1.getText().toString(),
                            BlankFragmentReisterEditText2.getText().toString()
                    });
                    //-------------------------
                    Domiss();

                    Toast.makeText(getActivity(), "恭喜《老司机》购车成功", Toast.LENGTH_SHORT).show();
                }


            }
        });
        builder.setNegativeButton("在想想!!", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity(), "欢迎下次光临", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }


}
